package org.ricks.log;

import java.io.IOException;
import java.util.Map;

/**
 * @author chenwei
 * @Description:日志管理器，容器启动如果要使用日志系统
 * @date 2023/1/916:29
 */
public class LogManager {
    private static final LogManager INSTANCE = new LogManager();

    private static final LogPath path = new LogPath(LogConfig.getPath());
    /**
     * 日志输出管理器
     */
    private static LogFileWriter fileWriter;
    /**
     * 异步日志调度器
     */
    private final AsyncLoggerDisruptor asyncLoggerDisruptor;


    /**
     * 私有化构建函数
     */
    private LogManager() {
        this.asyncLoggerDisruptor = new AsyncLoggerDisruptor();
    }

    /**
     * 日志功能初始化.
     *
     */
    public static LogFileWriter init() {
        return fileWriter;
    }

    static {
        try {
            fileWriter = new LogFileWriter(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 日志功能安全停止.
     */
    public static void shutdown() {
        getAsyncLoggerDisruptor().shutdown();
        try {
            fileWriter.flushAndClose();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 获取异步日志调度器
     *
     * @return 日志调度器
     */
    static AsyncLoggerDisruptor getAsyncLoggerDisruptor() {
        return INSTANCE.asyncLoggerDisruptor;
    }

}
